package com.more.offer.exercise.jdkproxy01;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author pinoc
 * @date 2021/8/9
 * @description
 */
public class MyInvocationHandler implements InvocationHandler {

	private Object target;

	public MyInvocationHandler() {
	}

	public MyInvocationHandler(Object target) {
		this.target = target;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("start proxy..do some thing");
		Object invoke = method.invoke(target, args);
		System.out.println("end proxy.. do some thing");
		return invoke;
	}

	public IBusinessService getProxy() {
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		Class<?>[] interfaces = target.getClass().getInterfaces();
		return (IBusinessService)Proxy.newProxyInstance(contextClassLoader, interfaces, this);
	}
}
